﻿using System;
using System.Configuration;
using System.ComponentModel;
using System.Diagnostics;

namespace TipokHostManager.Configuration
{
    class LocalSettings
    {
        private string _computerName = string.Empty;
        private System.Configuration.Configuration _config = ConfigurationManager.OpenExeConfiguration(typeof(LocalSettings).Assembly.Location);
        public event PropertyChangedEventHandler PropertyChanged;
        private PerformanceCounter _counterMachineCpu;
        private PerformanceCounter _counterMachineMemory;
        private PerformanceCounter _counterWorkerCpu;
        private PerformanceCounter _counterWorkerMemory;
        private string _masterServiceName;
        private string _masterBasePath = AppDomain.CurrentDomain.BaseDirectory;
        private string _workerPath;
        private string _csMasertServiceName = string.Empty;
        private string _csMasterDirectory = string.Empty;
        private string _csWorkerDirectory = string.Empty;
        private string _rtwtsMasterDirectory = string.Empty;
        private string _rtwtsWorkerDirectory = string.Empty;
        private string _rtwtsMasertServiceName = string.Empty;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public LocalSettings()
        {
            if (this._config.AppSettings.Settings["ComputerName"] != null)
            {
                this._computerName = this._config.AppSettings.Settings["ComputerName"].Value;
            }
            else
            {
                this._computerName = ConfigurationManager.AppSettings["ComputerName"];
            }
            if (string.IsNullOrEmpty(this._computerName))
            {
                this._computerName = Environment.MachineName;
            }
            if (this._config.AppSettings.Settings["ServiceName"] != null)
            {
                this._masterServiceName = this._config.AppSettings.Settings["ServiceName"].Value;
            }
            else
            {
                this._masterServiceName = Process.GetCurrentProcess().ProcessName;
            }
            if (this._config.AppSettings.Settings["WorkPath"] != null)
            {
                this._workerPath = this._masterBasePath + this._config.AppSettings.Settings["WorkPath"] + @"\";
            }
            else
            {
                this._workerPath = this._masterBasePath + this._masterServiceName + @"\";
            }
            this._csMasertServiceName = GetSettingValue("CSMasterServiceName");
            this._csMasterDirectory = GetSettingValue("CSMasterDirectory");
            this._csWorkerDirectory = GetSettingValue("CSWorkerDirectory");

            this._rtwtsMasertServiceName = GetSettingValue("RTWTSMasterServiceName");
            this._rtwtsMasterDirectory = GetSettingValue("RTWTSMasterDirectory");
            this._rtwtsWorkerDirectory = GetSettingValue("RTWTSWorkerDirectory");
            this._counterMachineCpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            this._counterMachineMemory = new PerformanceCounter("Memory", "Available MBytes", "");
            this._counterWorkerCpu = new PerformanceCounter("Process", "% Processor Time", this._masterServiceName.Replace(".exe", ""));
            this._counterWorkerMemory = new PerformanceCounter("Memory", "Available MBytes", this._masterServiceName.Replace(".exe", ""));
        }
        public string MasterPipeName
        {
            get
            {
                return string.Format("MasterPipe_" + Process.GetCurrentProcess().Id, new object[0]);
            }
        }

        private string GetSettingValue(string key)
        {
            string _value = string.Empty;
            if (this._config.AppSettings.Settings[key] != null)
            {
                _value = this._config.AppSettings.Settings[key].Value;
            }
            else
            {
                _value = ConfigurationManager.AppSettings[key];
            }
            return _value;
        } 

        public string MaseterServiceName
        {
            get
            {
                return this._masterServiceName;
            }
        }

        public string WorkerPaht
        {
            get
            {
                return this._workerPath;
            }
        }

        public string ComputerName
        {
            get
            {
                return this._computerName;
            }
            set
            {
                if (this._config.AppSettings.Settings["ComputerName"] != null)
                {
                    this._config.AppSettings.Settings["ComputerName"].Value = value;
                }
                else
                {
                    this._config.AppSettings.Settings.Add("ComputerName", value);
                }
                this._config.Save();
                this._computerName = value;
                this.OnPropertyChanged("ComputerName");
            }
        }
        public string CSMasertServiceName
        {
            get
            {
                return _csMasertServiceName;
            }
        }

        public string CSMasterDirectory
        {
            get
            {
                return _csMasterDirectory;
            }
        }
        public string CSWorkerDirectory
        {
            get
            {
                return _csWorkerDirectory;
            }
        }
        public string RTWTSMasertServiceName
        {
            get
            {
                return _rtwtsMasertServiceName;
            }
        }
        public string RTWTSMasterDirectory
        {
            get
            {
                return _rtwtsMasterDirectory;
            }
        }
        public string RTWTSWorkerDirectory
        {
            get
            {
                return _rtwtsWorkerDirectory;
            }
        }
    }
}
